/*
 * #!
 * Ontopia Engine
 * #-
 * Copyright (C) 2001 - 2013 The Ontopia Project
 * #-
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * !#
 */

package net.ontopia.topicmaps.query.impl.basic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.ontopia.topicmaps.query.core.InvalidQueryException;
import net.ontopia.topicmaps.query.core.ParsedQueryIF;
import net.ontopia.topicmaps.query.core.QueryResultIF;
import net.ontopia.topicmaps.query.parser.TologQuery;
import net.ontopia.topicmaps.query.parser.Variable;

/**
 * INTERNAL: Class used to represent parsed queries. The class wraps a
 * query executer and a tolog query intance (as generated by the
 * parser). The actual query execution is delegated to the query
 * processor.
 */
public class ParsedQuery implements ParsedQueryIF {

  protected TologQuery query;
  protected QueryProcessor processor;
  
  public ParsedQuery(QueryProcessor processor, TologQuery query) {
    this.processor = processor;
    this.query = query;
  }

  public List getClauses() {
    return query.getClauses();
  }
  
  /// ParsedQueryIF implementation [the class does not implement the interface]
  
  @Override
  public List<String> getSelectedVariables() {
    return getVariables(query.getSelectedVariables());
  }

  @Override
  public Collection<String> getAllVariables() {
    return getVariables(query.getAllVariables());
  }
  
  @Override
  public Collection<String> getCountedVariables() {
    return getVariables(query.getCountedVariables());
  }

  @Override
  public List<String> getOrderBy() {
    return getVariables(query.getOrderBy());
  }

  @Override
  public boolean isOrderedAscending(String name) {
    return query.isOrderedAscending(name);
  }

  @Override
  public QueryResultIF execute() throws InvalidQueryException {
    return processor.execute(query);
  }

  @Override
  public QueryResultIF execute(Map<String, ?> arguments) throws InvalidQueryException {
    return processor.execute(query, arguments);
  }
  
  /// Object implementation

  @Override
  public String toString() {
    return query.toString();
  }

  protected List<String> getVariables(Collection<Variable> varnames) {
    List<String> results = new ArrayList<String>(varnames.size());
    Iterator<Variable> iter = varnames.iterator();
    while (iter.hasNext()) {
      results.add(iter.next().getName());
    }
    return results;
  }

  public Object[] getVariableTypes(String varname) {
    return (Object[]) query.getVariableTypes().get(varname);
  }  
}
